import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class ConsoleInputToFile {
    private static final String FILE_NAME = "output.txt"; 
    private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm:ss"); 

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        BufferedWriter writer = null;

        try {
            writer = new BufferedWriter(new FileWriter(FILE_NAME, true)); 

            System.out.println("Enter text (type 'quit' to exit):");

            while (true) {
                String input = scanner.nextLine();

                if ("quit".equalsIgnoreCase(input)) {
                    break;
                }

                String timestamp = TIME_FORMAT.format(new Date()) + " "; 
                writer.write(timestamp + input); 
                writer.newLine(); 
                writer.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null) {
                    writer.close(); 
                }
                scanner.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
